package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.pojo.TbGoodsKz;
import com.qf.pojo.TbOrder;
import com.qf.pojo.TbOrderdetail;
import com.qf.utils.DBUtils;
import com.qf.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public void addOrder(String oid, long uid, long money, String aid) {
        String sql = SqlUtils.getSql("order.adds");
        try {
            queryRunner.insert(DBUtils.getConnection(), sql, new ScalarHandler<>(),oid,uid,money,aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public long addOrderDetail(String oid, long kzid, int num, long money) {
        String sql = SqlUtils.getSql("orderdeteil.adds");

        try {
            long detailId = queryRunner.insert(DBUtils.getConnection(), sql, new ScalarHandler<>(), oid, kzid, num, money);
            return detailId;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public String updateStaus2Success(String id) {
        String sql = SqlUtils.getSql("sess");
        try {
            queryRunner.update(DBUtils.getConnection(), sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return sql;
    }

    @Override
    public String findStatusById(String id) {
        String sql = SqlUtils.getSql("order.status");
        try {
            String result = queryRunner.query(DBUtils.getConnection(), sql, new ScalarHandler<>(), id);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
    @Override
    public List<TbOrder> showorder(long uid) {
        String sql = SqlUtils.getSql("showorder");
        try {
            List<TbOrder> query = queryRunner.query(DBUtils.getConnection(), sql, new BeanListHandler<>(TbOrder.class), uid);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TbOrder showkz(String id) {
        String sql = SqlUtils.getSql("shouwkz");
        try {
            TbOrder query = queryRunner.query(DBUtils.getConnection(), sql, new BeanHandler<>(TbOrder.class), id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TbOrderdetail> showde(String id) {
        String sql = SqlUtils.getSql("showdetral");
        try {
            List<TbOrderdetail> query = queryRunner.query(DBUtils.getConnection(), sql, new BeanListHandler<>(TbOrderdetail.class), id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deletegouwu(String id) {
        String sql = SqlUtils.getSql("deletegouwu");
        try {
            queryRunner.update(DBUtils.getConnection(), sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
